package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.buried.SelfUtils;
import com.cardbaobao.cardbabyclient.ui.activity.LoansDetailActivity;
import com.cardbaobao.cardbabyclient.model.SpeedOrProduct;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.cardbaobao.cardbabyclient.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Created by Jiangzx
 * on 2017/6/13
 */

public class LoanFilterProductAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private long enterTime;
    private String currentPageId;
    private String prePageId;

    private Context mContext;
    private List<SpeedOrProduct.LoanListBean> mData;

    private static final int TYPE_NO_DATA = 1;
    private static final int TYPE_CONTENT = 2;

    public LoanFilterProductAdapter(Context context, List<SpeedOrProduct.LoanListBean> mData) {
        this.mContext = context;
        this.mData = mData;
    }

    public void setBuriedInfo(long enterTime, String currentPageId, String prePageId){
        this.enterTime=enterTime;
        this.currentPageId=currentPageId;
        this.prePageId=prePageId;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (viewType == TYPE_NO_DATA) {
            View itemView = inflater.inflate(R.layout.item_no_data, parent, false);
            return new NoDataHolder(itemView);
        }
        if (viewType == TYPE_CONTENT) {
            View itemView = inflater.inflate(R.layout.item_speed_loan_rv, parent, false);
            return new ContentHolder(itemView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentHolder) {
            ContentHolder contentHolder = (ContentHolder) holder;
            final SpeedOrProduct.LoanListBean homeTabBean = mData.get(position);
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL + homeTabBean.getImage(),
                    contentHolder.pic,
                    R.mipmap.img_default_square);
            if (homeTabBean.getTopTag() != null) {
                for (String s : homeTabBean.getTopTag()) {
                    switch (s) {
                        case "新":
                            contentHolder.ivNew.setVisibility(View.VISIBLE);
                            break;
                        case "热":
                            contentHolder.ivHot.setVisibility(View.VISIBLE);
                            break;
                        case "荐":
                            contentHolder.ivReCommand.setVisibility(View.VISIBLE);
                            break;
                        default:
                            contentHolder.ivReCommand.setVisibility(View.GONE);
                            contentHolder.ivHot.setVisibility(View.GONE);
                            contentHolder.ivNew.setVisibility(View.GONE);
                            break;
                    }
                }
            }
            contentHolder.tvTitle.setText(homeTabBean.getName());
            contentHolder.tvMarketingOne.setText(homeTabBean.getFeature());
            if (homeTabBean.getBottomTag() != null) {
                List<String> source=homeTabBean.getBottomTag();
                List<String> temp=new ArrayList<>();
                for(String s:source){
                    if(!TextUtils.isEmpty(s)){
                        temp.add(s);
                    }
                }
                if(temp.size()>3){
                    contentHolder.flowLayout.setAdapter(
                            new FlowCommonAdapter(mContext, temp.subList(0,3), R.layout.item_speed_loan_label_gv));
                }else{
                    contentHolder.flowLayout.setAdapter(
                            new FlowCommonAdapter(mContext, temp, R.layout.item_speed_loan_label_gv));
                }

            }
            contentHolder.tvApplyCount.setText(String.valueOf(homeTabBean.getTotalApply()));
            contentHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SelfUtils.buriedClickData(Self.Action.app_click_loan_filter_item,
                            Self.Action.app_click_loan_filter_item_des,
                            enterTime,
                            currentPageId,prePageId);
                    Intent mIntent = new Intent();
                    mIntent.setClass(mContext, LoansDetailActivity.class);
                    mIntent.putExtra(LoansDetailActivity.LOANS_ID_S,
                            homeTabBean.getID() + "");
                    mIntent.putExtra(LoansDetailActivity.LOANS_NAME_S,
                            homeTabBean.getName());
                    mIntent.putExtra(LoansDetailActivity.FROM_PAGE_S,
                            "贷款-人群分类");
                    mIntent.putExtra(BaseActivity.PRE_PAGE_ID_S,
                            currentPageId);
                    mContext.startActivity(mIntent);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return (mData==null||mData.size()==0)?TYPE_NO_DATA:TYPE_CONTENT;
    }

    @Override
    public int getItemCount() {
        return (mData==null||mData.size()==0)?1:mData.size();
    }

    private class ContentHolder extends RecyclerView.ViewHolder{
        private ImageView pic;
        private ImageView ivReCommand;
        private ImageView ivHot;
        private ImageView ivNew;
        private TextView tvTitle;
        private TextView tvMarketingOne;
        private FlowLayout flowLayout;
        private TextView tvApplyCount;
        private ContentHolder(View itemView){
            super(itemView);
            pic= (ImageView) itemView.findViewById(R.id.iv_pic);
            ivReCommand = (ImageView) itemView.findViewById(R.id.iv_recommend);
            ivHot= (ImageView) itemView.findViewById(R.id.iv_hot);
            ivNew= (ImageView) itemView.findViewById(R.id.iv_new);
            tvTitle= (TextView) itemView.findViewById(R.id.tv_title);
            tvMarketingOne= (TextView) itemView.findViewById(R.id.tv_marketing_one);
            flowLayout= (FlowLayout) itemView.findViewById(R.id.fl_label);
            tvApplyCount= (TextView) itemView.findViewById(R.id.tv_number);
        }
    }

    private class NoDataHolder extends RecyclerView.ViewHolder{
        private NoDataHolder(View itemView){
            super(itemView);
            itemView.setVisibility(View.VISIBLE);
        }
    }
}
